#include <bits/stdc++.h>
#define int long long
using namespace std;
int T, n, a[200005];
int dp[2007][2007][2];
signed main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i=1;i<=n;i++) cin >> a[i];
        if (n <= 2000) {
            for (int i=1;i<=n;i++)
                for (int j=1;j<=i;j++) dp[i][j][0] = dp[i][j][1] = 0;
            int ans = 0;
            for (int i=1;i<=n;i++)
                for (int j=0;j<i;j++) {
                    ans = max(ans, max(dp[i][j][0], dp[i][j][1]));
                    dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][0]+a[i+1]*(a[i+1]==a[i])); // 0 0
                    dp[i+1][i][1] = max(dp[i+1][i][1], dp[i][j][0]+a[i+1]*(a[i+1]==a[j])); // 0 1
                    dp[i+1][i][0] = max(dp[i+1][i][0], dp[i][j][1]+a[i+1]*(a[i+1]==a[j])); // 1 0
                    dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][1]+a[i+1]*(a[i+1]==a[i])); // 1 1
                }
            cout << ans << "\n";
        } else cout << "0\n";
    }
    return 0;
}